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!!"
		"!!!!!!!!!!!!)3%B5V.4!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!04U^XN\\?GZO<F*35AY/$2%2%*#1E*35F)S-D3UN,AY/$N,3U"
		"U.$1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"@8V^````````````````````````````````````````````````%2%2!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!P<W^`P\\_````````"
		"````````````````````````````````````5V.4!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Q-$^`@X````````````````````````````"
		"`````````````````P\\_FJ;7!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!4%R-````````````````````````````````````````````````"
		"````V>86!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!R-4%"
		"`````````````````````````````````````````````````````@X^&256!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'\"A9`P\\_````````````````"
		"`````````````````````````````````````@X^BY?(!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!.T=X````````````````````````````````````"
		"````````````````````````P<W^!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		"!!!!!!!!'RM<````````````````````````````````````````````````````"
		"````````:76F!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'2E:"
		"VN<7^`@X`````````````````P\\_````````````````````_PL[I;'B!!!!!!!!"
		"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#1E**C9G4U^0FJ;7"
		"@(R]U>(2T-T-R]@(H*S=G:G:GJK;@X^`+3EJ\"A9'!!!!!!!!!!!!!!!!!!!!!!!!"
		"";

void GraphicTest::init() {
	m_icon = new Icon(28,&header_data[0]);
}
void GraphicTest::display() {
	Serial.println(testFillScreen());
	delay(500);

//	Hardware.progmemPrint(PSTR("Text                     "));
//	Serial.println(testText());
//	delay(3000);

	Hardware.progmemPrint(PSTR("Lines                    "));
	Serial.println(testLines(CYAN));
	delay(500);

	Hardware.progmemPrint(PSTR("Horiz/Vert Lines         "));
	Serial.println(testFastLines(RED, BLUE));
	delay(500);

	Hardware.progmemPrint(PSTR("Rectangles (outline)     "));
	Serial.println(testRects(GREEN));
	delay(500);

	Hardware.progmemPrint(PSTR("Rectangles (filled)      "));
	Serial.println(testFilledRects(YELLOW, MAGENTA));
	delay(500);

	Hardware.progmemPrint(PSTR("Circles (filled)         "));
	Serial.println(testFilledCircles(10, MAGENTA));

	Hardware.progmemPrint(PSTR("Circles (outline)        "));
	Serial.println(testCircles(10, WHITE));
	delay(500);

	Hardware.progmemPrint(PSTR("Triangles (outline)      "));
	Serial.println(testTriangles());
	delay(500);

	Hardware.progmemPrint(PSTR("Triangles (filled)       "));
	Serial.println(testFilledTriangles());
	delay(500);

	Hardware.progmemPrint(PSTR("Rounded rects (outline)  "));
	Serial.println(testRoundRects());
	delay(500);

	Hardware.progmemPrint(PSTR("Rounded rects (filled)   "));
	Serial.println(testFilledRoundRects());
	delay(500);

//	for (uint8_t rotation = 0; rotation < 4; rotation++) {
//		Graphics.setRotation(rotation);
//		testText();
//		delay(2000);
//	}
	Appregistry.jumpToMenu();

}
unsigned long GraphicTest::testFillScreen() {
	unsigned long start = micros();
	Graphics.fillScreen(BLACK);
	Graphics.fillScreen(RED);
	Graphics.fillScreen(GREEN);
	Graphics.fillScreen(BLUE);
	Graphics.fillScreen(BLACK);
	return micros() - start;
}

unsigned long GraphicTest::testText() {
	Graphics.fillScreen(BLACK);
	unsigned long start = micros();
	Graphics.setCursor(0, 0);
	Graphics.setTextColor(WHITE);
	Graphics.setTextSize(1);
	Graphics.println(PSTR("Hello World!"));
	Graphics.setTextColor(YELLOW);
	Graphics.setTextSize(2);
	Graphics.println(1234.56);
	Graphics.setTextColor(RED);
	Graphics.setTextSize(3);
	Graphics.println(0xDEADBEEF, HEX);
	Graphics.println();
	Graphics.setTextColor(GREEN);
	Graphics.setTextSize(5);
	Graphics.println(PSTR("Groop"));
	Graphics.setTextSize(2);
	Graphics.println(PSTR("I implore thee,"));
	Graphics.setTextSize(1);
	Graphics.println(PSTR("my foonting turlingdromes."));
	Graphics.println(PSTR("And hooptiously drangle me"));
	Graphics.println(PSTR("with crinkly bindlewurdles,"));
	Graphics.println(PSTR("Or I will rend thee"));
	Graphics.println(PSTR("in the gobberwarts"));
	Graphics.println(PSTR("with my blurglecruncheon,"));
	Graphics.println(PSTR("see if I don't!"));
	return micros() - start;
}

unsigned long GraphicTest::testLines(uint16_t color) {
	unsigned long start, t;
	int x1, y1, x2, y2, w = Graphics.width(), h = Graphics.height();

	Graphics.fillScreen(BLACK);

	x1 = y1 = 0;
	y2 = h - 1;
	start = micros();
	for (x2 = 0; x2 < w; x2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	x2 = w - 1;
	for (y2 = 0; y2 < h; y2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	t = micros() - start; // fillScreen doesn't count against timing

	Graphics.fillScreen(BLACK);

	x1 = w - 1;
	y1 = 0;
	y2 = h - 1;
	start = micros();
	for (x2 = 0; x2 < w; x2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	x2 = 0;
	for (y2 = 0; y2 < h; y2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	t += micros() - start;

	Graphics.fillScreen(BLACK);

	x1 = 0;
	y1 = h - 1;
	y2 = 0;
	start = micros();
	for (x2 = 0; x2 < w; x2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	x2 = w - 1;
	for (y2 = 0; y2 < h; y2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	t += micros() - start;

	Graphics.fillScreen(BLACK);

	x1 = w - 1;
	y1 = h - 1;
	y2 = 0;
	start = micros();
	for (x2 = 0; x2 < w; x2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);
	x2 = 0;
	for (y2 = 0; y2 < h; y2 += 6)
		Graphics.drawLine(x1, y1, x2, y2, color);

	return micros() - start;
}

unsigned long GraphicTest::testFastLines(uint16_t color1, uint16_t color2) {
	unsigned long start;
	int x, y, w = Graphics.width(), h = Graphics.height();

	Graphics.fillScreen(BLACK);
	start = micros();
	for (y = 0; y < h; y += 5)
		Graphics.drawFastHLine(0, y, w, color1);
	for (x = 0; x < w; x += 5)
		Graphics.drawFastVLine(x, 0, h, color2);

	return micros() - start;
}

unsigned long GraphicTest::testRects(uint16_t color) {
	unsigned long start;
	int n, i, i2, cx = Graphics.width() / 2, cy = Graphics.height() / 2;

	Graphics.fillScreen(BLACK);
	n = min(Graphics.width(), Graphics.height());
	start = micros();
	for (i = 2; i < n; i += 6) {
		i2 = i / 2;
		Graphics.drawRect(cx - i2, cy - i2, i, i, color);
	}

	return micros() - start;
}

unsigned long GraphicTest::testFilledRects(uint16_t color1, uint16_t color2) {
	unsigned long start, t = 0;
	int n, i, i2, cx = Graphics.width() / 2 - 1, cy = Graphics.height() / 2 - 1;

	Graphics.fillScreen(BLACK);
	n = min(Graphics.width(), Graphics.height());
	for (i = n; i > 0; i -= 6) {
		i2 = i / 2;
		start = micros();
		Graphics.fillRect(cx - i2, cy - i2, i, i, color1);
		t += micros() - start;
		// Outlines are not included in timing results
		Graphics.drawRect(cx - i2, cy - i2, i, i, color2);
	}

	return t;
}

unsigned long GraphicTest::testFilledCircles(uint8_t radius, uint16_t color) {
	unsigned long start;
	int x, y, w = Graphics.width(), h = Graphics.height(), r2 = radius * 2;

	Graphics.fillScreen(BLACK);
	start = micros();
	for (x = radius; x < w; x += r2) {
		for (y = radius; y < h; y += r2) {
			Graphics.fillCircle(x, y, radius, color);
		}
	}

	return micros() - start;
}

unsigned long GraphicTest::testCircles(uint8_t radius, uint16_t color) {
	unsigned long start;
	int x, y, r2 = radius * 2, w = Graphics.width() + radius, h =
			Graphics.height() + radius;

	// Screen is not cleared for this one -- this is
	// intentional and does not affect the reported time.
	start = micros();
	for (x = 0; x < w; x += r2) {
		for (y = 0; y < h; y += r2) {
			Graphics.drawCircle(x, y, radius, color);
		}
	}

	return micros() - start;
}

unsigned long GraphicTest::testTriangles() {
	unsigned long start;
	int n, i, cx = Graphics.width() / 2 - 1, cy = Graphics.height() / 2 - 1;

	Graphics.fillScreen(BLACK);
	n = min(cx, cy);
	start = micros();
	for (i = 0; i < n; i += 5) {
		Graphics.drawTriangle(cx, cy - i, // peak
		cx - i, cy + i, // bottom left
		cx + i, cy + i, // bottom right
		Graphics.color565(0, 0, i));
	}

	return micros() - start;
}

unsigned long GraphicTest::testFilledTriangles() {
	unsigned long start, t = 0;
	int i, cx = Graphics.width() / 2 - 1, cy = Graphics.height() / 2 - 1;

	Graphics.fillScreen(BLACK);
	start = micros();
	for (i = min(cx,cy); i > 10; i -= 5) {
		start = micros();
		Graphics.fillTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
				Graphics.color565(0, i, i));
		t += micros() - start;
		Graphics.drawTriangle(cx, cy - i, cx - i, cy + i, cx + i, cy + i,
				Graphics.color565(i, i, 0));
	}

	return t;
}

unsigned long GraphicTest::testRoundRects() {
	unsigned long start;
	int w, i, i2, cx = Graphics.width() / 2 - 1, cy = Graphics.height() / 2 - 1;

	Graphics.fillScreen(BLACK);
	w = min(Graphics.width(), Graphics.height());
	start = micros();
	for (i = 0; i < w; i += 6) {
		i2 = i / 2;
		Graphics.drawRoundRect(cx - i2, cy - i2, i, i, i / 8,
				Graphics.color565(i, 0, 0));
	}

	return micros() - start;
}

unsigned long GraphicTest::testFilledRoundRects() {
	unsigned long start;
	int i, i2, cx = Graphics.width() / 2 - 1, cy = Graphics.height() / 2 - 1;

	Graphics.fillScreen(BLACK);
	start = micros();
	for (i = min(Graphics.width(), Graphics.height()); i > 20; i -= 6) {
		i2 = i / 2;
		Graphics.fillRoundRect(cx - i2, cy - i2, i, i, i / 8,
				Graphics.color565(0, i, 0));
	}

	return micros() - start;
}

App *graphicTest = new GraphicTest();


